package com.orange.cetback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orange.cetback.pojo.entity.MedalLevel;
import com.orange.cetback.pojo.entity.UserCheckins;
import com.orange.cetback.mapper.UserCheckinsMapper;
import com.orange.cetback.service.MedalLevelService;
import com.orange.cetback.service.UserCheckinsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.cetback.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 用户签到记录表 服务实现类
 * </p>
 *
 * @author chenenjun
 * @since 2025-02-21
 */
@Service
public class UserCheckinsServiceImpl extends ServiceImpl<UserCheckinsMapper, UserCheckins> implements UserCheckinsService {

    @Autowired
    private UserService userService;
    @Autowired
    private MedalLevelService medalLevelService;

    @Override
    public List<UserCheckins> list(Long currentId) {
        LambdaQueryWrapper<UserCheckins> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCheckins::getUserId, currentId);
        List<UserCheckins> userCheckins = this.baseMapper.selectList(wrapper);
        return userCheckins;
    }

    @Override
    public MedalLevel signIn(Long currentId) {
        UserCheckins userCheckins = UserCheckins.builder()
                .userId(currentId)
                .checkinDate(LocalDate.now())
                .build();
        this.baseMapper.insert(userCheckins);
        //更新积分
        userService.UpdateIntegral(5);

        //检查是否获得勋章
        MedalLevel medalLevel = medalLevelService.checkAndGrantMedals();
        return medalLevel;
    }
}
